var teacherid = getQueryVariable('techerid');
var studentid = getQueryVariable('studentid');
var studentName = getQueryVariable('studentName'); // 2018-10-7 chenbin: 获取学员名
var orderId = getQueryVariable('orderId');
var timeType = getQueryVariable('type'); // 2018-10-7 chenbin: 占用或保留

layui.use(['form', 'layedit', 'laydate'], function () {
    var form = layui.form
    var laydate = layui.laydate;

    // 2018-9-27 chenbin: 渲染日期
    laydate.render({
        elem: '#auditionTime', done: function (value, date, endDate) {
            loadDatas(); // 2018-10-7 chenbin: 当试听日期选择完毕时加载该老师所有时段
        }
    });
});

jQuery(document).ready(function () {
    $('#kit-search-more').on('click', function () {
        $('.kit-search-mored').toggle();
    });

    var col = [
        {
            title: '时间段',
            field: 'name',
            align: 'center',
            valign: 'middle'
        },
        {
            title: '周一',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_1"><input disabled="disabled" id="' + row.id + '_1" timeid="' + row.id + '" weekid="1"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周二',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_2"><input disabled="disabled" id="' + row.id + '_2" timeid="' + row.id + '" weekid="2"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周三',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_3"><input disabled="disabled" id="' + row.id + '_3" timeid="' + row.id + '" weekid="3"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周四',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_4"><input disabled="disabled" id="' + row.id + '_4" timeid="' + row.id + '" weekid="4"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周五',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_5"><input disabled="disabled" id="' + row.id + '_5" timeid="' + row.id + '" weekid="5"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周六',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_6"><input disabled="disabled" id="' + row.id + '_6" timeid="' + row.id + '" weekid="6"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        },
        {
            title: '周日',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<div id="div_' + row.id + '_7"><input disabled="disabled" id="' + row.id + '_7" timeid="' + row.id + '" weekid="7"name="wait" type="checkbox" >&nbsp;&nbsp;</div>';
            }
        }];

    // var day = new Date();
    // var today = new Array(7,1,2,3,4,5,6);
    // var week = today[day.getDay()];
    // alert(week);
    // var cols = [
    //     col[0],
    //     col[week]
    // ];


    //------------------------------------------加载列表
    $('#_table').bootstrapTable({
        url: baseUrl + '/periods',
        'method': "GET",
        locale: 'zh-CN',
        detailView: false,
        sidePagination: "server", //服务端处理分页
        striped: true,//隔行变色
        undefinedText: "",
        pagination: true,
        singleSelect: false,
        pageSize: 200,//pageList[0],
        cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pageNumber: 1,
//		pageList: pageList,
        search: false, //不显示 搜索框
        uniqueId: "id", //每一行的唯一标识，一般为主键列
        showColumns: false, //不显示下拉框（选择显示的列）
        undefinedText: '',
        columns: col,
        queryParams: function (params) {
            return {
                pageNum: this.pageNumber,
                pageSize: this.pageSize,
                sortOrder: params.order
            }
        },
        onLoadSuccess: function () {
            loadDatas(); // 2018-10-7 chenbin: 加载该老师所有时段
        }
    });

})
var loadDatas = function () {
    //url:'/teacherPeriods',
    // 2018-10-7 chenbin: GET方式请求'/teacherPeriods/getPeriodUsed'，同时发送数据teacherid，成功时执行function(data)
    // var auditionTime=$("#auditionTime").val();
    // $.get(baseUrl + '/teacherPeriods/auditionTime/'+studentid,{teacherid: teacherid}, function (data) {
    //     $("#auditionTime").val(data.data);
    // });
    // $("#auditionTime").val(new Date().Format("yyyy-MM-dd"));

    // var day = new Date();
    var atime=$("#auditionTime").val();
    var day = new Date($("#auditionTime").val());
    var today = new Array(7,1,2,3,4,5,6);
    var week = today[day.getDay()];

        $.get(baseUrl + '/teacherPeriods/new', {teacherid: teacherid}, function (data) {
        console.log(data);
        datas = data.rows;


            var map = {}; // Map map = new HashMap();
            for (var i = 0; i < datas.length; i++) {
                var obj = datas[i];
                var status = obj.status;
                var checkid = obj.checkid;
                if (status != '可用' && status != '占用') {
                    if (map[checkid] != null) {
                        var arr = map[checkid];
                        arr.push(obj);
                        map[checkid] = arr;
                    } else {
                        var arr = new Array();
                        arr.push(obj);
                        map[checkid] = arr;
                    }
                }
            }


        for (var i = 0; i < datas.length; i++) {
            var obj = datas[i];
            var htm;
            // 2018-10-7 chenbin: 增加判断，仅可选“可用时段”
            if (obj.status == '可用') {
                if (week != obj.week) {
                    htm = '<input id="' + obj.checkid + '" timeid="' + obj.timeid + '" weekid="' + obj.week + '"name="oldNew" type="checkbox" disabled="disabled">请选择';
                } else {
                    htm = '<input id="' + obj.checkid + '" timeid="' + obj.timeid + '" weekid="' + obj.week + '"name="old" type="checkbox">请选择';
                }
            } else if(obj.status=='占用'){
                htm = '<input id="' + obj.checkid + '" objid="' + obj.id + '" class="blue" name="oldNew" type="checkbox" checked  disabled="disabled">' +
                    '<font size="3" color="#FF0000">'+obj.useMsg+'</font>';
            }
            $('#div_' + obj.checkid).html(htm);
            $('#div_' + obj.checkid).parent().css("background-color",'#9ACCF1');
        }

        var color;
            for (var key in map) {
                var arr = map[key];
                var htmA = "";
                for (var i = 0; i < arr.length; i++) {
                    var objA = arr[i];
                    if (objA.status == '试听' && objA.studentid != studentid) {
                        if (objA.date != atime) {
                            if (week != objA.week) {
                                htmA += '<input id="' + objA.checkid + '" timeid="' + objA.timeid + '" weekid="' + objA.week + '"name="oldNew" type="checkbox" disabled="disabled">请选择'+'<br>';
                            } else {
                                htmA += '<input id="' + objA.checkid + '" timeid="' + objA.timeid + '" weekid="' + objA.week + '"name="old" type="checkbox">请选择'+'<br>';
                            }
                        } else {
                            htmA += '<input id="' + objA.checkid + '" objid="' + objA.id + '" class="yellow" name="oldNew" type="checkbox" checked  disabled="disabled">' +
                                '<font color="#FF0000">' + objA.useMsg + '</font>'+'<br>';
                        }
                    } else if (objA.status == '试听' && objA.studentid == studentid) {
                        if (week != objA.week) {
                            htmA += '<input id="' + objA.checkid + '" timeid="' + objA.timeid + '" weekid="' + objA.week + '"name="old" type="checkbox" disabled="disabled">请选择'+'<br>';
                        } else {
                            color='#85BCBF';
                            htmA += '<input id="' + objA.checkid + '" timeid="' + objA.timeid + '" weekid="' + objA.week + '"name="old" checked type="checkbox">请选择'+'<br>';
                        }
                    }
                }

                for (var i = 0; i < arr.length; i++) {
                    var objB = arr[i];
                    if (objA.status == '占用') {
                        htmA += '<input id="' + objB.checkid + '" objid="' + objB.id + '" class="blue" name="oldNew" type="checkbox" checked  disabled="disabled">' +
                            '<font  color="#FF0000">' + objA.useMsg + '</font><br>';
                    } else if(objB.status == '保留'){
                        if (htmA == "") {
                            if (week == objB.week) {
                                htmA += '<input id="' + objB.checkid + '" timeid="' + objB.timeid + '" weekid="' + objB.week + '"name="old" type="checkbox">请选择' + '<br>';
                            } else {
                                htmA += '<input id="' + objB.checkid + '" timeid="' + objB.timeid + '" weekid="' + objB.week + '"name="old" type="checkbox" disabled>请选择' + '<br>';
                            }
                        }
                        htmA += '<input id="' + objB.checkid + '" objid="' + objB.id + '" class="blue" name="oldNew" type="checkbox" checked  disabled="disabled">' +
                            '<font  color="#FF0000">' + objA.useMsg + '</font><br>';
                    }
                }
                // if (arr.length == 1 && arr[0].status == '试听') {
                //     htmA += '<input id="' + objB.checkid + '" timeid="' + objB.timeid + '" weekid="' + objB.week + '"name="old" type="checkbox">请选择' + '<br>';
                // }
                $('#div_' + key).html(htmA);
                if(color!=null && color!=""){
                    $('#div_' + key).parent().css("background-color", color);
                    color="";
                }else {
                    $('#div_' + key).parent().css("background-color", '#9ACCF1');
                }
            }

    });
}

// 2018-10-6 chenbin: 保存数据
var callback_save = function () {
    var auditionTime=$("#auditionTime").val();
    if(auditionTime==""){
    	alert("请选择试听时间");
    	return;
    }
    var adds = [], retentions = [], dels = [];
    var objs = [];
    var news = $('input:checkbox[name=wait]:checked').each(function (i) {
        var obj = {};
        obj.teacherid = teacherid;
        obj.checkid = $(this).attr('timeid') + '_' + $(this).attr('weekid');
        obj.week = $(this).attr('weekid');
        obj.timeid = $(this).attr('timeid');
        obj.useMsg = '可用时段';
        obj.retention = '';
        adds.push(obj);
    });

    $('input:checkbox[name=old]:not(:checked)').each(function(i) {
        var obj = {};
        obj.teacherid = teacherid;
        obj.checkid = $(this).attr('id');
        obj.week = $(this).attr('weekid');
        obj.timeid = $(this).attr('timeid');

        obj.status = "试听";
        obj.useMsg = studentName + obj.status;
        obj.retention = 'false';
        obj.date = $("#auditionTime").val();
        obj.studentid = studentid;
        obj.sdStatus = 'delete';
        objs.push(obj);
    });

    var index=0;
    $('input:checkbox[name=old]:checked').each(function (i) {
        index ++;
        var obj = {};
        obj.teacherid = teacherid;
        obj.checkid = $(this).attr('id');
        obj.week = $(this).attr('weekid');
        obj.timeid = $(this).attr('timeid');

        obj.status = "试听";
        obj.useMsg = studentName + "试听";
        obj.retention = 'false';
        obj.date = $("#auditionTime").val();
        obj.studentid = studentid;
        obj.sdStatus = 'add';
        objs.push(obj);
    });
    console.log(objs);
    var datas = [teacherid, JSON.stringify(objs),auditionTime];

    return datas;
//	return $('.layui-form').serialize();
}


var callback_check = function () {
    var index=0;
    $('input:checkbox[name=old]:checked').each(function (i) {
        index ++;
    });
    return index;
}
var check = function () {

}

